#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;
int prexor[N];

void solve() {
	int a, b; cin >> a >> b;
	int y = prexor[a - 1] ^ b;
	if (y == a) cout << a + 2 << '\n';
	else if (y == 0) cout << a << '\n';
	else cout << a + 1 << '\n';

}
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	for (int i = 1; i <= 2e5; i++) prexor[i] = prexor[i - 1] ^ i;
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}